package root.electronclass.electronclass.other

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.ActivityPictureBinding

/**
 * 通用图片展示
 */
class PictureActivity : Activity() {


    lateinit var binding: ActivityPictureBinding
    private var list = arrayListOf<String>()
    private var position = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_picture)
        val path = intent.getStringArrayListExtra("Path") as ArrayList<String>
        list.addAll(path)
        position = intent.getIntExtra("position", 0)
        binding.imageView.setImageURI(list[position])
        onClick()
    }

    private fun onClick() {
        binding.bgBack.setOnClickListener {
            finish()
        }

        binding.ivLeft.setOnClickListener {
            if (list.size == 1) {

            } else if (position == 0) {
                position = list.size - 1
            } else {
                position--
            }
            binding.imageView.setImageURI(list[position])
        }

        binding.ivRight.setOnClickListener {
            if (list.size == 1) {

            } else if (position == list.size - 1) {
                position = 0
            } else {
                position++
            }
            binding.imageView.setImageURI(list[position])
        }
    }

    companion object {

        @JvmStatic
        fun startActivity(context: Context,list: ArrayList<String>,position: Int) {
            val intent = Intent(context, PictureActivity::class.java)
            intent.putStringArrayListExtra("Path",list)
            intent.putExtra("position",position)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }

}